#!/bin/ksh
#
# build.sh
#
# Master makefile script for compiling all libraries and programs
# for a LINUX operating system.
#
# Practical Algorithms for Image Analysis
#
# Copyright (c) 1997, 1998, 1999 M. Seul, L. O'Gorman, M. J. Sammon
# 
# NOTE: for a successful compilation
# the following are suggested:
# 1) gcc version 2.7.2
# 2) LINUX kernel version 2.0.0
#

checkforce()
{
        if [ $force -eq 1 ]
        then
                print "$me: $1: make clean"
                make clean
        fi
}

checkmake()
{
	if [ "$?" != "0" ]
	then
		echo "$me: MAKE FAILED BUILDING $dir"
		if [ $ignoreErrors -eq 0 ]; then
			cd $HERE
			exit 2
		fi
	fi
}

usage()
{
    echo "usage: $me [-dgi] [-S dir] [force|clean]" 1>&2 
    echo "-d = print directory build sequence on stdout and exit" 1>&2 
    echo "-g = compile and link with -g" 1>&2 
    echo "-i = ignore errors" 1>&2 
    echo '-S dir = start build at directory named dir' 1>&2 
}

me=build.sh
HERE=`pwd`

libs="libtiff libimage libip"

progs="\
ch_2.1/histstats \
ch_2.2/histex \
ch_2.2/histexx \
ch_2.2/histramp \
ch_2.3/combine \
ch_2.3/imgarith \
ch_2.3/imgbool \
ch_2.3/inv \
ch_2.4/imgrotate \
ch_2.4/xscale \
ch_2.5/rgb2gray \
ch_3.1/xconv \
ch_3.2/lpfltr \
ch_3.2/medfltr \
ch_3.3/bc \
ch_3.4/peak \
ch_3.5/bcd \
ch_3.6/subsample \
ch_3.6/subsampleNI \
ch_3.7/multires \
ch_3.8/xcorr \
ch_3.9/binarize \
ch_3.9/threshe \
ch_3.9/threshk \
ch_3.9/threshm \
ch_3.9/thresho \
ch_4.1/cellog \
ch_4.1/morph \
ch_4.10/hough \
ch_4.2/kfill \
ch_4.3/contour \
ch_4.3/xah \
ch_4.3/xcc \
ch_4.3/xcp \
ch_4.3/xrg \
ch_4.4/xfm \
ch_4.4/xpm \
ch_4.5/xbdy \
ch_4.6/xph \
ch_4.7/thin \
ch_4.8/thinw \
ch_4.9/globalfeats \
ch_4.9/imggrid \
ch_4.9/profile \
ch_5.1/pcc \
ch_5.1/pccde \
ch_5.1/pccdump \
ch_5.1/pccfeat \
ch_5.2/linefeat \
ch_5.2/linerid \
ch_5.2/linexy \
ch_5.2/structfeat \
ch_5.2/structrid \
ch_5.3/fitpolyg \
ch_5.4/fitcrit \
ch_5.5/fitline \
ch_5.6/fitspline \
ch_5.7/eh_seg \
ch_5.7/eh_sgl \
ch_5.7/xsgll \
ch_6.1/spp \
ch_6.1/xvor \
ch_6.2/dpp \
ch_6.2/xptstats \
ch_6.2/xvora \
ch_6.3/xsgt \
ch_6.4/xknn \
ch_7.1/powspec \
ch_7.2/fltrfreq \
"

# Build libraries first
buildSequence="$libs $progs"
beginDir=
force=0
target=all
makeopts=
here=$HERE
ignoreErrors=0
while getopts ':digS:?' arg
do
    case $arg in
    [d])
		for i in $buildSequence
		do
			print "$i"
		done
		exit 0;;
    [i])
		ignoreErrors=1;;
    [g])
		G="-g";;
    [S])
		beginDir=$OPTARG;;
    '?')
        usage
		exit 0;;
    *)
        usage
		exit 2;;
    esac
done
shift `expr $OPTIND - 1`
if [ "$1" = "" ]
then
	print "$me: starting a normal build"
else
	while [ "$1" != "" ]
	do
		case $1 in
		force|FORCE)
			force=1
			print "$me: starting forced complete build"
			;;
		clean)
			target=clean
			;;
		*)
			print "$me: unrecognized command-line parameter $1 (ignored)"
			;;
		esac
		shift
	done
fi

building=0
if [ "$beginDir" = "" ]; then
	beginDir=libtiff
fi

for dir in $buildSequence
do
	if [ $building -eq 0 ]; then
		if [ "$beginDir" = "$dir" ]; then
			building=1
		fi
	fi
	if [ $building -eq 0 ]; then
		echo "$me: skipping $dir" 1>&2
	else
		if [ -d $dir ]; then
			cd $dir
			checkforce $dir
			make $makeopts $target
			checkmake
		fi
	fi
	cd $here
done
